$(function(){
    var intOverallDelta = 0;
    var id = 0;
    var $scroll = $("#chapter");
    var $getMar = $scroll.css("margin-top");
    var ind = $("#content > div").size();
    var sections = [];
    var $paging = $("#content .paging");
    var $sectionID = $("#content > div:first-child").attr("id");

    for (var i=0;i < ind ; i++ ){
        var sectionNum =$("#content > div").eq(i).children(".section").size();
        if(i == 0){
            sections[i] = sectionNum;
        }else{
            sections[i] = sections[i-1] + sectionNum;
        }
    }

    $("#content").mousewheel(function(objEvent, intDelta){
        // mousewheel UP
        if (intDelta > 0){
            if (intOverallDelta != 0){
                if ($("#content > div").is(":animated")){
                    return false;
                } else {
                    intOverallDelta--;
                    var top = intOverallDelta * 700;

                    $paging.hide();
                    $("#content > div").animate({
                        "top": "-" + top  + "px"
                    },1000, function(){
                        if(intOverallDelta == 0){
                            $paging.hide();
                        } else {
                            $paging.show().html((intOverallDelta+1) + " / " + sections[sections.length-1]);
                        }
                    });
                    
                    if(intOverallDelta == sections[id-1] -1){
                        id--;
                        leftSection();
                    }
                }
            } else {
                if ($("#content > div").is(":animated") || $sectionID == "chapter00"){
                    return false;
                } else {
                    var prevHref = $("#leftSection ul li a.on").parent().prev().children().attr("href");
                    location.href = prevHref;
                }
                
            }
            
            //arrow btn - cover page show
            if(intOverallDelta == 0){
                $(".chapter .section:first-child").find(".down a").show();
                $(".down").hover(function(){$(".down a").show();},function(){$(".down a").show();});
            }
        }

        // mousewheel Down
        else if (intDelta < 0){
            //arrow btn over 
            if (navigator.userAgent.match(/iPhone|iPod|iPad|Android|Windows CE|BlackBerry|Symbian|Windows Phone|webOS|Opera Mini|Opera Mobi|POLARIS|IEMobile|lgtelecom|nokia|SonyEricsson/i) == null && navigator.userAgent.match(/LG|SAMSUNG|Samsung/) == null){
                $(".up a, .down a").hide();
                $(".up").hover(function(){$(".up a").show();}, function(){$(".up a").hide();});
                $(".down").hover(function(){$(".down a").show();}, function(){$(".down a").hide();});
            }

            if (intOverallDelta != sections[sections.length-1]-1)   {
                if ($("#content > div").is(":animated")){
                    return false;
                } else {
                    intOverallDelta++;
                    var top = intOverallDelta * 700;
                    
                    $paging.hide();
                    $("#content > div").animate({
                        "top": "-" + top  + "px"
                    }, 1000, function(){
                        $paging.show().html((intOverallDelta+1) + " / " + sections[sections.length-1]);
                    });
                    
                }
            }else {
                if ($("#content > div").is(":animated") || $sectionID == "chapter20"){
                    return false;
                } else {
                    var nextHref = $("#leftSection ul li a.on").parent().next().children().attr("href");
                    location.href = nextHref;
                }
                
            }
        }

    });

    $("#leftSection ul li a").click(function(){
        var thisHref = $("#leftSection ul li a.on").attr("href");
        if($(this).attr("href") == thisHref){
            return false;
        }
    });
    

    $(".down a").click(function(){
        //arrow btn over 
        if (navigator.userAgent.match(/iPhone|iPod|iPad|Android|Windows CE|BlackBerry|Symbian|Windows Phone|webOS|Opera Mini|Opera Mobi|POLARIS|IEMobile|lgtelecom|nokia|SonyEricsson/i) == null && navigator.userAgent.match(/LG|SAMSUNG|Samsung/) == null){
            $(".up a, .down a").hide();
            $(".up").hover(function(){$(".up a").show();}, function(){$(".up a").hide();});
            $(".down").hover(function(){$(".down a").show();}, function(){$(".down a").hide();});
        }

        if (intOverallDelta != sections[sections.length-1]-1)   {
            if ($("#content > div").is(":animated")){
                return false;
            } else {
                intOverallDelta++;
                var top = intOverallDelta * 700;
                
                $paging.hide();
                $("#content > div").animate({
                    "top": "-" + top  + "px"
                }, 1000, function(){
                    $paging.show().html((intOverallDelta+1) + " / " + sections[sections.length-1]);
                });
                
            }
        }else {
            if ($("#content > div").is(":animated")){
                return false;
            } else {
                var nextHref = $("#leftSection ul li a.on").parent().next().children().attr("href");
                location.href = nextHref;
            }
            
        }
    });

    $(".up a").click(function(){
        if (intOverallDelta != 0)   {
            if ($("#content > div").is(":animated")){
                return false;
            } else {
                intOverallDelta--;
                var top = intOverallDelta * 700;

                $paging.hide();
                $("#content > div").animate({
                    "top": "-" + top  + "px"
                },1000, function(){
                    if(intOverallDelta == 0){
                        $paging.hide();
                    } else {
                        $paging.show().html((intOverallDelta+1) + " / " + sections[sections.length-1]);
                    }
                });
                
                if(intOverallDelta == sections[id-1] -1){
                    id--;
                    leftSection();
                }
            }
        }else {
            if ($("#content > div").is(":animated")){
                return false;
            } else {
                var prevHref = $("#leftSection ul li a.on").parent().prev().children().attr("href");
                location.href = prevHref;
            }
            
        }

        //arrow btn - cover page show
        if(intOverallDelta == 0){
            $(".chapter .section:first-child").find(".down a").show();
            $(".down").hover(function(){$(".down a").show();},function(){$(".down a").show();});
        }
    });

    $("#loading").delay(700).fadeOut();
    $("#wrap").fadeIn(500);

});

$(function(){
    /* web */
    if (navigator.userAgent.match(/iPhone|iPod|iPad|Android|Windows CE|BlackBerry|Symbian|Windows Phone|webOS|Opera Mini|Opera Mobi|POLARIS|IEMobile|lgtelecom|nokia|SonyEricsson/i) == null && navigator.userAgent.match(/LG|SAMSUNG|Samsung/) == null) {

    }
    /* mobile */
    else{
       $(function(){
           $("#content").mousewheel(function(objEvent, intDelta){
               // mousewheel UP
               if (intDelta > 0){
                   //arrow btn - cover page show
                   if(intOverallDelta == 0){
                       $(".chapter .section:first-child").find(".down a").show();
                       //$(".down").hover(function(){$(".down a").show();},function(){$(".down a").show();});
                   }
               }
               // mousewheel Down
               else if (intDelta < 0){
                   //arrow btn over
                       $(".up a, .down a").show();
                       //$(".up").hover(function(){$(".up a").show();}, function(){$(".up a").show();});
                       //$(".down").hover(function(){$(".down a").show();}, function(){$(".down a").show();});
                   }
               });
           });
       }
   });